package com.yuanian.ecsplus.bean;

import com.yuanian.ecsplus.meta.Meta;
import com.yuanian.ecsplus.meta.vo.MetaVo;
import com.yuanian.ecsplus.model.EcsModel;
import com.yuanian.ecsplus.model.entity.BaseModel;
import com.yuanian.ecsplus.session.BatchCrudSession;
import com.yuanian.ecsplus.session.CrudSession;
import com.yuanian.ecsplus.session.QuerySession;
import com.yuanian.ecsplus.cache.CacheManager;
import com.yuanian.ecsplus.meta.entity.MetaEntity;
import com.yuanian.ecsplus.platform.PlatformSupport;
import com.yuanian.ecsplus.platform.SpringContextBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 普通模式的SQL查询和CURD
 * @param <T>
 */
public class CommonModel<T extends EcsModel> implements QuerySession<T>, CrudSession<T>, BatchCrudSession<T> {

    //查询实体类字节
    private Class<T> clz;

    //平台的支撑类
    private PlatformSupport platformSupport;

    public CommonModel(Class<T> clz) {
        this.clz = clz;
        this.platformSupport = SpringContextBean.getBean(PlatformSupport.class);
    }

    /**
     * 根据objectid发布实体类
     * @param objectId
     * @return
     */
    public void issueByObjectId(String objectId) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        HashMap map = new HashMap();
        map.put(metaEntity.getMetaClass().getIdField().getField().getName(), objectId);
        this.platformSupport.issueBoData(metaEntity.getMetaClass().getApiName(), null, map);
    }

    /**
     * 根据objectid删除实体类
     * @param objectId
     * @return
     */
    public int deleteByObjectId(String objectId) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        return this.platformSupport.deleteByBoMainId(metaEntity.getMetaClass().getApiName(), null, objectId);
    }

    /**
     * 根据objectid查找单个实体类
     * @param objectId
     * @return
     */
    public T selectByObjectId(String objectId) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        return (T) metaEntity.selectBoDataByBoMainId(objectId);

    }

    /**
     * 根据dataId查找单个实体类
     * @param dataId
     * @return
     */
    public T selectByDataId(String dataId) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        return (T) metaEntity.selectBoDataByBoMainId(dataId);
    }

    /**
     * 根据SQL查找多个实体类
     * @param sql
     * @return
     */
    public List<T> selectList(String sql) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        return metaEntity.findList(sql);
    }

    //todo 加排序

    @Override
    public Map selectMap(String sql) {
        return null;
    }


    @Override
    public List<Map> selectListMap(String sql) {
        return null;
    }

    /**
     * 根据SQL查找单个实体类
     * @param sql
     * @return
     */
    public T selectOne(String sql) {
        List<T> list = selectList(sql);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 批量插入
     * @param list
     */
    public int batchInsert(List<T> list) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        return metaEntity.batchInsert(list);
    }

    /**
     * 批量修改
     * @param list
     */
    public void batchUpdate(List<T> list) {

    }

    /**
     * 批量删除
     * @param list
     */
    public  int batchDelete(List<T> list) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        return metaEntity.batchDelete(list);
    }

    /**
     * 批量发布
     * @param list
     * @param
     */
    public  void batchIssue(List<T> list) {

    }

    /**
     * 批量保存，不存在就插入，存在就修改
     * @param list
     * @param
     */
    public  void batchSave(List<T> list) {
        Meta metaEntity = CacheManager.getInstance().getMetaEntityOrVo(clz);
        metaEntity.batchSave(list);
    }

}
